package org.helgoboss.janana_tray_log_opener

import org.helgoboss.osgi_additions._
import org.helgoboss.janana.Janana
import java.awt.MenuItem
import java.awt.event.{ActionEvent, ActionListener}
import org.helgoboss.janana_tray.{JananaTray, JananaTrayAddOn}
import java.awt.Desktop
import java.io.File

class JananaTrayLogOpener extends OsgiProvider with OsgiConsumer with OsgiLogging {
    def janana = requiredService[Janana]
        
    whenBundleActive {
        trayAddOn.providesService[JananaTrayAddOn]
    }
    
    val trayAddOn = new JananaTrayAddOn {
        lazy val logOpenerItem = {
            val logOpenerListener = new ActionListener {
                def actionPerformed(e: ActionEvent) {
                    val logFile = new File(janana.getUserDataDir, "var/log/janana.log")
                    Desktop.getDesktop.open(logFile)
                }
            }
                    
            val tmp = new MenuItem("Open Log File")
            tmp.addActionListener(logOpenerListener)
            tmp
        }
        
        def addToJananaTray(tray: JananaTray) {
            tray.getIcon.getPopupMenu.add(logOpenerItem)
        }

        def removeFromJananaTray(tray: JananaTray) {
            tray.getIcon.getPopupMenu.remove(logOpenerItem)
        }
    }
}